package com.miyake.client.profile;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.miyake.client.DoctorsUserService;
import com.miyake.client.DoctorsUserServiceAsync;
import com.miyake.client.datamanage.NodeProperty;
import com.miyake.client.tools.ClickableLabel;
import com.miyake.client.tools.ImageList;
import com.miyake.client.tools.ImageListElement;
import com.miyake.client.tools.OkCancelDialog;
import com.miyake.client.tools.ShowDialog;
import com.miyake.client.tools.SmallButton;
import com.miyake.client.tools.ImageList.CommunityListHandler;

public class ProfileMain extends Composite {
	protected DoctorsUserServiceAsync service = DoctorsUserService.Util.getInstance();

	private UserPropertyList propertyList;
//	private VerticalPanel friendPanel;
	private String currentKey;
	private boolean me = true;
	private VerticalPanel mainPanel;
	private VerticalPanel facePanel;
	private VerticalPanel profilePanel;
	private VerticalPanel friendPanel;
	private VerticalPanel communityPanel;
	private VerticalPanel dataPanel;
	
	public ProfileMain() {
		mainPanel = new VerticalPanel();
		initWidget(mainPanel);
		mainPanel.setWidth("100%");
		
		getMyProfile();
	}

	private void createUserProfile(UserProperty property) {
		currentKey = property.getKey();
		
		mainPanel.clear();
		if (!me) {
			mainPanel.add(createToolbar());
		}
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		mainPanel.add(horizontalPanel);
		horizontalPanel.setWidth("100%");
				
		horizontalPanel.add(createFacePanel(property));
		
		VerticalPanel verticalPanel_1 = new VerticalPanel();
		horizontalPanel.add(verticalPanel_1);
		verticalPanel_1.setWidth("100%");
		
		verticalPanel_1.add(createProfile(property));
		
		mainPanel.add(createFriendList());
		mainPanel.add(createDataList(property));
	}

	private Widget createDataList(UserProperty property) {
		if (dataPanel == null) {
			dataPanel = new VerticalPanel();
		}
		dataPanel.clear();
		dataPanel.add(new OpenDataList(property.getKey()));
		return dataPanel;
	}

	private VerticalPanel createToolbar() {
		VerticalPanel panel = new VerticalPanel();
		panel.add(new ClickableLabel("My Profile", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				getMyProfile();
			}
		}));
		HorizontalPanel toolBar = new HorizontalPanel();
		panel.add(toolBar);
		toolBar.add(new SmallButton("Remove", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				removeFriend(currentKey);
			}
		}));
		toolBar.add(new SmallButton("Send Message", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {

			}
		}));
		
		return panel;
	}

	private VerticalPanel createFacePanel(UserProperty property) {
		if (facePanel == null) {
			facePanel = new VerticalPanel();
			facePanel.setBorderWidth(1);
		}
		else {
			facePanel.clear();
		}
		facePanel.add(new FacePanel(property, me));
		return facePanel;
	}

	protected void removeFriend(final String currentKey2) {
		new ShowDialog() {
			@Override
			protected void onOK(String text) {
				service.removeFriend(currentKey2, new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
					}

					@Override
					public void onSuccess(Void result) {
						getMyProfile();
					}
				});
			}

			@Override
			protected DialogBox getDialogBox() {
				return new OkCancelDialog();
			}
			
		};

	}

	private VerticalPanel createProfile(UserProperty property) {
		if (profilePanel == null) {
			profilePanel = new VerticalPanel();
		}
		else {
			profilePanel.clear();
		}
		propertyList = new UserPropertyList(me);
		profilePanel.add(propertyList);
		propertyList.setUserProperty(property);
		return profilePanel;
	}

	private void getMyProfile() {
		service.getMyProperty(new AsyncCallback<UserProperty>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(UserProperty result) {
				me = true;
				createUserProfile(result);
			}
		});	
	}
	
	protected VerticalPanel createFriendList() {
		if (friendPanel == null) {
			friendPanel = new VerticalPanel();
		}
		else {
			friendPanel.clear();
		}
		FriendList flist = new FriendList(currentKey, me) {

			@Override
			void onUserChange(String key) {
				changeUser(key);
			}

			@Override
			void onFind() {
				showUserListDialog();
			}
			
		};
		friendPanel.add(flist);
		
		return friendPanel;
	}

	protected void changeUser(String key) {
		service.getUser(key, new AsyncCallback<UserProperty>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(UserProperty result) {
				me = false;
				createUserProfile(result);
			}
		});
	}

	protected void showUserListDialog() {
		UserListDialog2 dlg = new UserListDialog2();
		dlg.center();
		dlg.addCloseHandler(new CloseHandler<PopupPanel>() {
			@Override
			public void onClose(CloseEvent<PopupPanel> event) {
				if (!event.isAutoClosed()) {
					createFriendList();
				}
			}
		});
	}
}
